package com.xzl.time.controller;


import com.github.pagehelper.PageInfo;
import com.xzl.time.exceptions.MyException;
import com.xzl.time.pojo.Article;
import com.xzl.time.pojo.vo.UpdArticleVo;
import com.xzl.time.service.IArticleService;
import com.xzl.time.utils.MyEnum;
import com.xzl.tool.util.AssertUtil;
import com.xzl.tool.util.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2021-12-03
 */
@Api(tags = "文章管理")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @GetMapping("/")
    @ApiOperation("查询所有文章")
    public RespBean selectList(Integer current,Integer size,String userName){
        if(current == null || size == null) throw new MyException("参数异常！");
        PageInfo<Article> articlePageInfo = articleService.selectList(current, size,userName);
        return RespBean.success(articlePageInfo);
    }

    @GetMapping("/selectBiId")
    @ApiOperation("通过id查询文章")
    public RespBean selectBiId(String id){
        Article article = articleService.selectById(id);
        if(article == null){
            return RespBean.error("数据为空！");
        }
        return RespBean.success(article);
    }

    @PostMapping("/insertArticle")
    @ApiOperation("新增文章")
    public RespBean insertArticle(@Validated @RequestBody Article article){
        Article article1 = articleService.addArticle(article);
        return RespBean.success(article1);
    }

    @PostMapping("/updArticle")
    @ApiOperation("修改文章")
    public RespBean updArticle(@Validated @RequestBody UpdArticleVo article){
//        articleService.updArticleById(article);
        return RespBean.success();
    }

    @GetMapping("/getArticleByUserName")
    @ApiOperation("通过用户账号查询文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "",value = "",required = true,paramType = "query")
    })
    public RespBean getArticleByUserName(Integer current,Integer size,String userName){
        AssertUtil.isTrue(current == null || current <= 0,"参数异常！");
        AssertUtil.isTrue(size == null || size <= 0,"参数异常！");
        AssertUtil.isTrue(userName == null,"参数异常！");
        PageInfo<Article> articlePageInfo = articleService.getArticleByUserName(current,size,userName);
        return RespBean.success(articlePageInfo);
    }

    @GetMapping("/follow")
    @ApiOperation("关注作者")
    public RespBean follow(String authorId,String userId){
        Boolean flag = articleService.follow(authorId,userId);
        if (flag)
            return RespBean.success("已取消关注！",flag);
        return RespBean.success("已关注！",flag);
    }

    @GetMapping("/thumbsUp")
    @ApiOperation("点赞")
    public RespBean thumbsUp(String authorId,String userName,String articleId){
        AssertUtil.isTrue(authorId == null,"参数异常！");
        AssertUtil.isTrue(userName == null,"参数异常！");
        AssertUtil.isTrue(articleId == null,"参数异常！");
        int i = articleService.thumbsUp(authorId, userName, articleId);
        return RespBean.success(i);
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/redisTest")
    public RespBean redisTest(){
        String authorKey = MyEnum.ARTICLE_AUTHOR_.getValue() + "admin";
        Long size = redisTemplate.boundSetOps(authorKey).size();
        return RespBean.success(size);
    }
}
